package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IProduct;
import com.happyclub.lbj.service.IShopCar;
import com.happyclub.lbjdao.ShopCarDao;
import com.happyclub.paul.service.IPhotoLibraryService;
import com.happyclub.pojo.TbPhotoLibrary;
import com.happyclub.pojo.TbProduct;
import com.happyclub.pojo.TbShopcar;
import com.happyclub.utils.JsonResult;
import com.happyclub.utils.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/12
 */
@Service
public class ShopCarImpl implements IShopCar {
    @Autowired
    ShopCarDao shopCarDao;
    @Autowired
    IProduct iProduct;
    @Autowired
    IPhotoLibraryService iPhotoLibraryService;//paul

    @Transactional
    @Override
    public JsonResult saveProToShopCar(TbShopcar shopcar) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbShopcar save = shopCarDao.save(shopcar);
        jsonResult.setResult(save);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult delProFromShopCar(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        shopCarDao.deleteById(id);
        return jsonResult;
    }

    @Override
    public JsonResult findProFromShopCarByUserId(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbShopcar> byUserId = shopCarDao.findByUserId(id);
        if (byUserId.size()==0){
            jsonResult.setMsg("您的购物车没有商品...");
            return jsonResult;
        }
        for (TbShopcar sc:byUserId){
            JsonResult proDetail = iProduct.findProDetail(sc.getProductId());
            TbProduct tbProduct = (TbProduct) proDetail.getResult();
            String finaUrl = "";
            String imgUrl = tbProduct.getImgUrl();
            String[] photoId = imgUrl.split(",");
            for (String h : photoId){
                TbPhotoLibrary photo = iPhotoLibraryService.queryPhotoLibraryByPhotoId(h);
                String realUrl = photo.getPhotoUrl();
                finaUrl = finaUrl + realUrl + ",";
            }
            tbProduct.setImgUrl(finaUrl);
            sc.setProduct(tbProduct);
        }
        jsonResult.setResult(byUserId);
        return jsonResult;
    }
}
